'use strict';

/**
 * @ngdoc overview
 * @name zjzcVotingApp
 * @description
 * # zjzcVotingApp
 *
 * Main module of the application.
 */
angular
  .module('zjzcVotingApp', [
    'ngAnimate',
    'ngCookies',
    'ngResource',
    'ngRoute',
    'ngSanitize',
    'ngTouch',
    'ui.bootstrap',
    'angularFileUpload',
    'ui.router',
    'app.directive',
    'angular-md5'
  ])
  //拦截器
  .factory('appInterceptor', ['$q', function ($q) {
    //  var promise = $q.defer();
      return {
          request: function(config) {
              return config;
          },
          response: function(response) {
              return response;
          },
          responseError: function(response) {
          //    window.location.href = '#/login'
              return response;
          }
      };
  }])
  .config(['$stateProvider', '$urlRouterProvider', '$anchorScrollProvider', function ($stateProvider, $urlRouterProvider, $anchorScrollProvider) {
    
    //设置post方式  解决提交到后台后台接受不到参数
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
        //禁用get缓存
    if( !$httpProvider.defaults.headers.get ) {
        $httpProvider.defaults.headers.get = {};
    }
    // 禁用 IE AJAX 请求缓存
    $httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon, 26 Jul 1997 05:00:00 GMT';
    $httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
    $httpProvider.defaults.headers.get['Pragma'] = 'no-cache';

    //为后台能正常拦截angular异步请求,添加参数
    $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

    var param = function(obj) {
        var query = '', name, value, fullSubName, subName, subValue, innerObj, i;
        for (name in obj) {
            value = obj[name];
            // query += encodeURIComponent(name) + '=' + encodeURIComponent(JSON.stringify(value)) + '&';
            if (value instanceof Array) {
                for (i = 0; i < value.length; ++i) {
                    subValue = value[i];
                    fullSubName = name + '[' + i + ']';
                    innerObj = {};
                    innerObj[fullSubName] = subValue;
                    query += param(innerObj) + '&';
                }
            } else if (value instanceof Object) {
                for (subName in value) {
                    subValue = value[subName];
                    if(!subValue) {
                      if(subValue == 0) {

                      }else{
                        continue;
                      }
                    }
                    fullSubName = name + '.' + subName + '';
                    innerObj = {};
                    innerObj[fullSubName] = subValue;
                    query += param(innerObj) + '&';
                }
            } else if (value !== undefined && value !== null) {
                query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
         }
        }

        return query.length ? query.substr(0, query.length - 1) : query;
    }

    // Override $http service's default transformRequest
    $httpProvider.defaults.transformRequest = [function(data) {
        return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
    }];

    //添加拦截器
    $httpProvider.interceptors.push('appInterceptor');

    $anchorScrollProvider.disableAutoScrolling();

    $stateProvider
      //登录页面
      .state('login', {
        url: '/login',
        templateUrl: 'views/login.html',
        controller: 'LoginCtrl'
      })
      //主页面
      .state('index', {
        url: '/',
        templateUrl: 'views/index.html',
        controller: 'HeaderCtrl'
      })
      // 投票活动列表面
      .state('index.voteList', {
        url: 'voteList',
        templateUrl: 'views/vote/voteList.html',
        controller: 'voteListCtrl'
      })
      // 投票活动编辑面
      .state('index.voteEdit', {
        url: 'voteEdit/:id',
        templateUrl: 'views/vote/voteEdit.html',
        controller: 'voteEditCtrl'
      })
      // 投票活动编辑面
      .state('index.voteLog', {
        url: 'voteLog/:id',
        templateUrl: 'views/vote/voteLog.html',
        controller: 'voteLogCtrl'
      })
      // 投票活动编辑面
      .state('index.voteSet', {
        url: 'voteSet/:id',
        templateUrl: 'views/vote/voteSet.html',
        controller: 'voteSetCtrl'
      })

      .state('index.dashboard', {
        url: '/dashboard',
            templateUrl: 'views/dashboard.html',
            controller: 'dashboardCtrl'

      });


      // url 重定向
    $urlRouterProvider
     // .when('/voteEdit/:id', '/voteEdit/:id')
     // .when('/voteEdit','/voteEdit/')
      .otherwise('/');
  }]);
